// echo aux function only, should not compile alone.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using static Utils.CharacterMod;
namespace GongfaEffect;
//! 目前为止，我们知道，福利姬的素材id越接近1（眉毛是2），福利姬的魅力就越高。
//! 因此，我们只需要把福利姬的素材id往1这个方向靠拢就好。
public static partial class Utils {
    public const short lifeMax=11107; // 8295 * 100+服芝食玉100+服食仙丹50+天元一气15+仙胎仙骨15+天人骨血15=295，11107*295/100=32765 //好生寿长100不算，但这里不计算。
    public unsafe static void GetOffset(){
        EarlyCheck();
    }
    public unsafe static void younger(this GameData.Domains.Character.Character victim, int delta, sbyte month, GameData.Common.DataContext context){ // 人，年轻月数，当前月份，context
        // 小学奥数比高中奥数难多了
        // 你永远不知道你什么时候会不小心翻车
        var birth=(sbyte)_birthMonth.GetValue(victim);
        var remain=((month-birth+12)%12);
        int delta_age=(remain - delta +1200000)/12 - 100000; // 避免向0取整带来的误差
        var curr=(sbyte)((birth+delta) % 12);

        if(victim.GetCurrAge()+delta_age<16){
            short life=victim.GetBaseMaxHealth();
            if(life<=8295 || !victim.GetFeatureIds().Contains(261)){ // 好生寿长
                victim.SetBaseMaxHealth((short)(Math.Min(Math.Max(lifeMax,life),life+((month-curr+12)%12))), context);
            }
            curr=month;
            delta_age=16-victim.GetCurrAge();
        }
        if(delta_age!=0){
            victim.SetCurrAge((short)(victim.GetCurrAge()+delta_age),context);
            victim.SetActualAge((short)(victim.GetActualAge()+delta_age),context);
        }
        victim.birthMonth(curr, context);
    }
    public static MethodInfo ApplyNewClothingWhenAgeGroupChanges=typeof(GameData.Domains.Character.Character).GetMethod("ApplyNewClothingWhenAgeGroupChanges",(BindingFlags)(-1));
    public unsafe static void older(this GameData.Domains.Character.Character victim,int delta, sbyte month, GameData.Common.DataContext context){ // 人，变老月数，当前月份，context
        var delta_age=delta/12;
        sbyte curr=(sbyte)(((sbyte)_birthMonth.GetValue(victim))-(delta%12)-month);
        if(curr<0){
            curr+=12;
            delta_age++;
        }
        curr=(sbyte)((curr+month) % 12);
        if(delta_age!=0){
            var before=victim.GetCurrAge();
            var after=(short)(before+delta_age);
            victim.SetActualAge((short)(victim.GetActualAge()+delta_age),context);
            victim.SetCurrAge((short)(after),context);
            if(before<GlobalConfig.AgeAdult && after>=GlobalConfig.AgeAdult){
                ApplyNewClothingWhenAgeGroupChanges.Invoke(victim, new object[]{context,(short)(-1)});
            } else if(before<GlobalConfig.Instance.AgeBaby && after>=GlobalConfig.Instance.AgeBaby){
                ApplyNewClothingWhenAgeGroupChanges.Invoke(victim, new object[]{context,(short)(65)});
            }
        }
        victim.birthMonth(curr, context);
    }
}
